package model

import (
	"time"

	"gorm.io/gorm"
)

// Role 角色模型
type Role struct {
	ID        uint           `gorm:"primaryKey" json:"id"`
	Name      string         `gorm:"type:varchar(50);uniqueIndex;not null;comment:角色名称" json:"name"`
	Code      string         `gorm:"type:varchar(50);uniqueIndex;not null;comment:角色编码" json:"code"`
	Sort      int            `gorm:"type:int;default:0;comment:排序" json:"sort"`
	Status    int8           `gorm:"type:tinyint;default:1;comment:状态 0-禁用 1-启用" json:"status"`
	Remark    string         `gorm:"type:varchar(500);comment:备注" json:"remark"`
	CreatedAt time.Time      `json:"createdAt"`
	UpdatedAt time.Time      `json:"updatedAt"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`

	// 关联
	Resources        []*Resource          `gorm:"many2many:sys_role_resource;" json:"resources,omitempty"`
	DataPermission   *RoleDataPermission  `gorm:"foreignKey:RoleID" json:"dataPermission,omitempty"`
}

// TableName 指定表名
func (Role) TableName() string {
	return "sys_role"
}

// RoleResource 角色资源关联表
type RoleResource struct {
	RoleID     uint `gorm:"primaryKey"`
	ResourceID uint `gorm:"primaryKey"`
}

// TableName 指定表名
func (RoleResource) TableName() string {
	return "sys_role_resource"
}
